package com.chen.service;

import com.chen.bean.Blog;
import com.chen.bean.Page;
import com.chen.dao.BlogMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@Service
public class BlogService {

    @Resource
    BlogMapper blogMapper;

    public Integer insertContent(Blog blog){
        blogMapper.insertContent(blog);
        return blog.getId();
    }

    public void updateById(Blog blog) {
        blogMapper.updateById(blog);
    }

    public List<String> getLabelList(String username){
        return blogMapper.getLabelByUser(username);
    }

    public List<String> getTitleList(String label,String username){
        return blogMapper.getTitleByLabelAndUser(label,username);
    }

    public String loadContent(String title){
        return blogMapper.getContentByTitle(title);
    }

    public Blog loadBlog(String title){
        return blogMapper.getBlogDetails(title);
    }

    public List<Blog> loadBlogList(){
        return blogMapper.getBlogIntroduction();
    }

    public List<String> loadHotLabel(){
        return blogMapper.getLabelByNum();
    }

    public List<String> loadHotTitle(){
        return blogMapper.getTitleByNum();
    }

    public Page<List<Blog>> loadBlogListByLabel(String label , Integer pageNum, Integer pageSize){
        Integer total = blogMapper.getNumByLabel(label);
        Integer offset=(pageNum-1)*pageSize;
        HashMap map = new HashMap(){{
            put("label",label);
            put("offset",offset);
            put("size",pageSize);
        }};
        List<Blog> blogList = blogMapper.getBlogIntroByLabel(map);
        return new Page<>(total, blogList);
    }

    public boolean existStar(String title,String username){
        return blogMapper.existStar(title, username) >= 1;
    }

    public void starBlog(String title,String username){
        blogMapper.increaseStar(title,username);
    }

    public void cancelStar(String title,String username){
        blogMapper.deleteStar(title,username);
    }

    public Page<List<Blog>> loadStarBlog(String username,Integer pageNum,Integer PageSize){
        Integer total = blogMapper.getTotalNumStar(username);
        Integer offset=(pageNum-1)*PageSize;
        List<Blog> starBlogList = blogMapper.getStarBlogList(username, offset, PageSize);
        return new Page<>(total,starBlogList);
    }
}
